﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Model;
using System.Text;
namespace Manager
{
    public class XMLHelper
    {
        public string signDir = System.Configuration.ConfigurationManager.AppSettings["SignDir"].ToString();
        //public void CreateSignFile(HW_School school)
        //{
        //    string filename = signDir + "\\" + school.Name+ ".gdsx";
        //    if (!System.IO.File.Exists(filename))
        //    {
        //        RSAHelper rsa = new RSAHelper();
        //        string sign = rsa.WriteSign(school.SchoollCode);
        //        XmlDocument doc = new XmlDocument();
        //        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
        //        doc.AppendChild(dec);
        //        XmlNode root = doc.CreateElement("School");
        //        XmlElement code = doc.CreateElement("SchoolCode");
        //        code.InnerText = school.SchoollCode;
        //        //code.SetAttribute("SchoolCode", school.SchoollCode);
        //        root.AppendChild(code);

        //        XmlElement name = doc.CreateElement("SchoolName");
        //        name.InnerText = school.Name;
        //        //name.SetAttribute("SchoolCode", school.Name);
        //        root.AppendChild(name);

        //        XmlElement Type = doc.CreateElement("SchoolType");
        //        Type.InnerText = school.SchoolType;
        //        //name.SetAttribute("SchoolType", school.SchoolType);
        //        root.AppendChild(Type);

        //        XmlElement City = doc.CreateElement("City");
        //        City.InnerText = school.CityId;
        //        //name.SetAttribute("City", school.CityId);
        //        root.AppendChild(City);

        //        XmlElement District = doc.CreateElement("District");
        //        District.InnerText = school.DistrictCode;
        //        //name.SetAttribute("District", school.DistrictCode);
        //        root.AppendChild(District);

        //        XmlElement Charac = doc.CreateElement("Charac");
        //        Charac.InnerText = school.CityTown;
        //        //name.SetAttribute("Charac", school.CityTown);
        //        root.AppendChild(Charac);

        //        XmlElement Phone = doc.CreateElement("Phone");
        //        Phone.InnerText = school.OfficePhone;
        //        //name.SetAttribute("Phone", school.OfficePhone);
        //        root.AppendChild(Phone);

        //        XmlElement Mobile = doc.CreateElement("Mobile");
        //        Mobile.InnerText = school.MobilePhone;
        //        //name.SetAttribute("Mobile", school.MobilePhone);
        //        root.AppendChild(Mobile);

        //        XmlElement Sign = doc.CreateElement("Sign");
        //        Sign.InnerText = sign;
        //        //name.SetAttribute("Sign", school.SecCode);
        //        root.AppendChild(Sign);



        //        doc.AppendChild(root);
        //        doc.Save(filename);
        //    }
        //}
        public void CreateValidateFile(HW_School school)
        {
            HW_SchoolMergeLogManager mergeManager = new HW_SchoolMergeLogManager();
            HW_SchoolManager schoolManager = new HW_SchoolManager();
            XmlTextWriter xmlWriter;
            string strFilename = signDir + "\\" + school.SchoollCode + ".gdsx";
            xmlWriter = new XmlTextWriter(strFilename, Encoding.Default);//创建一个xml文档
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("School");

            xmlWriter.WriteStartElement("HW_School");
            xmlWriter.WriteAttributeString("CityId", school.CityId);
            xmlWriter.WriteAttributeString("CityName", school.CityName);
            xmlWriter.WriteAttributeString("DistrictCode", school.DistrictCode);
            xmlWriter.WriteAttributeString("DistrictName", school.DistrictName);
            xmlWriter.WriteAttributeString("SchoolCode", school.SchoollCode);
            xmlWriter.WriteAttributeString("Name", school.Name);
            xmlWriter.WriteAttributeString("Address", school.Address);
            xmlWriter.WriteAttributeString("Email", school.Email);
            xmlWriter.WriteAttributeString("LinkMan", school.LinkMan);
            xmlWriter.WriteAttributeString("MobilePhone", school.MobilePhone);
            xmlWriter.WriteAttributeString("OfficePhone", school.OfficePhone);
            xmlWriter.WriteAttributeString("State", school.Statute);
            xmlWriter.WriteAttributeString("SchoolCharacter", school.SchoolCharacter);
            xmlWriter.WriteAttributeString("SchoolType", school.SchoolType);
            if (string.IsNullOrEmpty(school.SecCode))
            {
                string showpass = SecrityPass.CreatePassword(8);
                String seccode = SecrityPass.GetPassword(showpass);
                schoolManager.Update(school.SchoollCode, "SecCode", seccode);
                school.SecCode = seccode;
            }
            xmlWriter.WriteAttributeString("Sign", school.SecCode);
            
            var dt = mergeManager.GetMergeSchool(school.SchoollCode);
            HW_School hwschool = null;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    hwschool = schoolManager.GetById(dt.Rows[i]["SchoolCode"].ToString());
                    xmlWriter.WriteStartElement("MergedSchool");
                    xmlWriter.WriteAttributeString("SchoolCode", hwschool.SchoollCode);
                    xmlWriter.WriteAttributeString("HostSchoolCode", school.SchoollCode);
                    xmlWriter.WriteEndElement();
                }
            }
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();
            xmlWriter.Close();



         
        }
        public void CreateSignFile(HW_School school)
        {
            //string filename = signDir + "\\" + school.Name + ".gdsx";
            string filename = signDir + "\\" + school.SchoollCode + ".gdsx";
            if (!System.IO.File.Exists(filename))
            {
                RSAHelper rsa = new RSAHelper();
                string sign = rsa.WriteSign(school.SchoollCode);
                School xmlSchool = new School()
                {
                    Address=school.Address,
                    CityId=school.CityId,
                    CityName=school.CityName,
                    CityTown=school.CityTown,
                    OrgCode=school.OrgCode,
                    DistrictCode=school.DistrictCode,
                    DistrictName=school.DistrictName,
                    Email=school.Email,
                    LinkMan=school.LinkMan,
                    MobilePhone=school.MobilePhone,
                    Name=school.Name,
                    OfficePhone=school.OfficePhone,
                    SchoolCharacter=school.SchoolCharacter,
                    SchoollCode=school.SchoollCode,
                    SchoolType=school.SchoolType,
                    SecCode=sign,
                    State=school.Statute,
                };
                School subschool = null;
                List<School> list = new List<School>();
                foreach (var s in school.Schools)
                {
                   subschool = new School()
                   {
                       Address = school.Address,
                       CityId = school.CityId,
                       CityName = school.CityName,
                       CityTown = school.CityTown,
                       OrgCode = school.OrgCode,
                       DistrictCode = school.DistrictCode,
                       DistrictName = school.DistrictName,
                       Email = school.Email,
                       LinkMan = school.LinkMan,
                       MobilePhone = school.MobilePhone,
                       Name = school.Name,
                       OfficePhone = school.OfficePhone,
                       SchoolCharacter = school.SchoolCharacter,
                       SchoollCode = school.SchoollCode,
                       SchoolType = school.SchoolType,
                       SecCode = sign,
                       State = school.Statute,
                   };
                   list.Add(subschool);
                }
                xmlSchool.Schools = list;
                XmlSerializer.XmlSerialize<School>(filename, xmlSchool);
            }
        }
        public void DeleteSignFile(HW_School school)
        {
            string filename = signDir + "\\" + school.Name + ".gdsx";
            if (System.IO.File.Exists(filename))
            {
                System.IO.File.Delete(filename);
            }
        }
        public void ResetSign(HW_School school)
        {
             string filename = signDir + "\\" + school.SchoollCode + ".xml";
             if (System.IO.File.Exists(filename))
             {
                 System.IO.File.Delete(filename);
                 CreateSignFile(school);
             }
        }

        //public void CreateValidateFile(HW_School school)
        //{
        //     string filename = signDir + "\\" + school.SchoollCode + ".gdsx";
        //     if (!System.IO.File.Exists(filename))
        //     {
        //         System.IO.File.Delete(filename);
        //     }
        //     HW_SchoolMergeLogManager mergeManager = new HW_SchoolMergeLogManager();
        //     HW_SchoolManager schoolManager=new HW_SchoolManager ();
        //         School xmlSchool = new School()
        //         {
        //             Address = school.Address,
        //             CityId = school.CityId,
        //             CityName = school.CityName,
        //             CityTown = school.CityTown,
        //             OrgCode = school.OrgCode,
        //             DistrictCode = school.DistrictCode,
        //             DistrictName = school.DistrictName,
        //             Email = school.Email,
        //             LinkMan = school.LinkMan,
        //             MobilePhone = school.MobilePhone,
        //             Name = school.Name,
        //             OfficePhone = school.OfficePhone,
        //             SchoolCharacter = school.SchoolCharacter,
        //             SchoollCode = school.SchoollCode,
        //             SchoolType = school.SchoolType,
        //             SecCode = school.SecCode,
        //             State = school.Statute,
        //         };
        //         if (string.IsNullOrEmpty(xmlSchool.SecCode))
        //         {
        //             string showpass = SecrityPass.CreatePassword(8);
        //             xmlSchool.SecCode = SecrityPass.GetPassword(showpass);
        //         }
        //         School subschool = null;
        //         HW_School hwschool = null;
        //         List<School> list = new List<School>();
        //         var dt = mergeManager.GetMergeSchool(school.SchoollCode);
        //    if(dt.Rows.Count>0)
        //    {
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            hwschool = schoolManager.GetById(dt.Rows[i]["SchoolCode"].ToString());
        //            subschool = new School()
        //            {
        //                Address = hwschool.Address,
        //                CityId = hwschool.CityId,
        //                CityName = hwschool.CityName,
        //                CityTown = hwschool.CityTown,
        //                OrgCode = hwschool.OrgCode,
        //                DistrictCode = hwschool.DistrictCode,
        //                DistrictName = hwschool.DistrictName,
        //                Email = hwschool.Email,
        //                LinkMan = hwschool.LinkMan,
        //                MobilePhone = hwschool.MobilePhone,
        //                Name = hwschool.Name,
        //                OfficePhone = hwschool.OfficePhone,
        //                SchoolCharacter = hwschool.SchoolCharacter,
        //                SchoollCode = hwschool.SchoollCode,
        //                SchoolType = hwschool.SchoolType,
        //                SecCode = hwschool.SecCode,
        //                State = hwschool.Statute,
        //            };
        //            list.Add(subschool);
        //        }
        //         xmlSchool.Schools = list;
        //     }
        //         XmlSerializer.XmlSerialize<School>(filename, xmlSchool);
        //}
    }
}